#ifndef __DATA_STORAGE__
#define __DATA_STORAGE__

#include <QObject>
#include <QtSql>

#include "common.h"

#define DB_SQL_DRIVER			"QSQLITE"
#define DB_COMPANY_NAME			"YINJIA"
#define DB_NAME					"YJ_DATABASE"
#define DB_USER_NAME			"user"
#define DB_PASSWORD				"password"

class DataStorage : public QObject
{
	Q_OBJECT

public:

	DataStorage(QString db_name, QObject *parent = nullptr);
	~DataStorage();

public:

	uint32_t db_append(TransInfo trans_info);
	bool db_query(QDate start, QDate stop);
	bool db_query_next(TransInfo &trans_info);
	bool db_query_nth(uint32_t idx, TransInfo &trans_info);
	void db_total_transaction(ClearInfo &clr_info);
	void db_clear_transaction();
	bool db_query_not_uploaded(QList<TransInfo> &trans_info_list);
    void db_transaction_uploaded(uint32_t id);

    void db_clear_sub_transaction();
	void db_append_sub_transaction(TransInfo trans_info);
    void db_merge_sub_transaction(TransInfo &trans_info);

	void db_append_failed_sub_transaction(TransInfo trans_info);

	bool db_op_query();
	bool db_op_query_next(OpInfo &op_info);
	bool db_op_query_nth(uint32_t idx, OpInfo &op_info);
	bool db_op_exists(QString op_name);
	bool db_op_exists(uint32_t finger_id);
	bool db_op_append(QString op_name, QString op_password, QString station_id, uint32_t finger_id);
	void db_op_update_password(QString op_name, QString op_password);
	bool db_op_update(QString op_name, QString op_password, QString station_id, uint32_t finger_id);
	bool db_op_authenticate(QString op_name, QString op_password);
	bool db_op_info(uint32_t finger_id, QString &op_name, QString &op_password);
	QString db_op_station_id(QString op_name);
	void db_op_delete(OpInfo op_info);

	void db_clr_append(ClearInfo clr_info);
	bool db_clr_query(QDate start, QDate stop);
	bool db_clr_query_next(ClearInfo &clr_info);
	bool db_clr_query_nth(uint32_t idx, ClearInfo &clr_info);

	void db_test();

private:

	void db_init();
	void db_table_init();

	void db_fetch_row(QSqlQuery *query, TransInfo &trans_info);
	void db_op_fetch_row(OpInfo &op_info);
	void db_clr_fetch_row(ClearInfo &clr_info);

private:

	QString m_database_name;
	QSqlQuery m_query;
	QSqlDatabase m_db;
};

#endif
